package com.acane.instagram.service.impl;

import com.acane.instagram.mapper.CollectionMapper;
import com.acane.instagram.mapper.PostMapper;
import com.acane.instagram.pojo.Collection;
import com.acane.instagram.pojo.Post;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    public Result getCollectionList(String userId) {
        List<Post> collectionList = collectionMapper.getCollectionList(userId);
        collectionList.forEach(post -> {
            post.setUrls(postMapper.selectPostWithImages(post.getId()));
            post.setUser(postMapper.getUserByPostId(post.getId()));
            post.setTags(postMapper.getTagsByPostId(post.getId()));
        });
        return Result.success(collectionList);
    }

    @Override
    public Result judgeCollected(String userId, String postId) {
        List<Collection> collections = collectionMapper.judgeCollected(userId, postId);
        if (!collections.isEmpty()) {
            return Result.success(true);
        }
        return Result.success(false);
    }

    @Override
    public Result cancelCollection(String userId, String postId) {
        return Result.success(collectionMapper.cancelCollection(userId, postId));
    }
}
